var __BATE__ = 'http://122.227.156.170:11224/';

//获取浏览器任务栏参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}
var codeV = getQueryString('code')
var expiresDate = new Date();
expiresDate.setTime(expiresDate.getTime() + (60 * 24 * 60 * 1000));

if (codeV != null && codeV != codeVal) {
	$.fn.cookie('codes', codeV, {
		expires: expiresDate
	});
}

var iscookie = document.cookie.indexOf("codes="); //得到分割的cookie名值
if (iscookie == -1) { //判断cookie是否存在
	$.fn.cookie('codes', codeV, {
		expires: expiresDate
	});
}
var codeVal = $.fn.cookie('codes');

function serverErr(a) {
	a = a || '服务器很忙！过三秒再操作吧！';
	stip(a);
}

var ifCode = function() {
	$.post(__BATE__ + 'api/access', {
		code: codeVal
	}, function(data) {
		if (data.ret_msg == '非法访问') {
			mui.openWindow({
				url: '/err.html',
				id: 'err.html'
			})
		}
	})
}
ifCode()
// 手机号正则
var mobileExp = /^(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/;
var checkTel = function(tel) {
	tel = tel || '';
	return (RegExp(mobileExp).test(tel));
};

/**/
mui('body').on('tap', '#search', function() {
	mui.openWindow({
		url: '/search.html',
		id: 'search.html'
	})
	$('#t-search').focus()
});

function log(a) {
	if (isobj(a)) {
		console.log(JSON.stringify(a));
		return false;
	}
}

function _int(a) {
	var a = parseInt(a);
	if (isNaN(a)) {
		a = 0;
	}
	return a;
}

function stip(a) {
	mui.toast(a);
}

function isobj(b) {
	if (typeof b === 'object') {
		var f = false;
		for (var k in b) {
			f = true;
			break;
		}
		return f;
	}
	return false;
}

var ssdd = function(i) {
	if ($(i).css('display') == 'none') {
		$(i).css('display', 'block');
	} else {
		$(i).css('display', 'none');
	}
}

function loadingShow(title) {
	loadingClose();
	title = title || '努力中...';
}

function loadingClose() {
	try {} catch (e) {}
}
var ifLogin = function(token) {

	$.ajax({
		url:__BATE__ + 'api/is_login ',
		data: {
			token: token
		},
		type: 'post',
		success: function(json) {
			if (json.ret_msg == "true") {
				return true;
			} else {
				//未登录
				mui.alert('您尚未登录，请登录！', '', function() {
					mui.openWindow('/user/login.html', 'login.html');
				});
//				return false;
			}


		},
		error: function(){
			serverErr();
		}
	});

}


var toLoginView = function() {
	openView('/user/login.html', 'login.html');
}
var toMain = function() {
	openView('/index.html', 'index.html');
}

var toRegView = function() {
	openView('/user/reg.html', 'reg.html');
}

var openView = function(htmlurl, htmlid, para) {
	if (!para) {
		para = {};
	}
	mui.openWindow({
		id: htmlid,
		url: htmlurl,
		show: {
			aniShow: true
		},
		styles: {
			popGesture: 'hide'
		},
		createNew: true,
		waiting: {
			autoShow: true,
			title: '努力中...'
		},
		extras: para
	});
}
var $$ = function (a){
	return document.getElementById(a);
}

mui('body').on('tap', '#fnav .mui-tab-item, .mui-table-view-cell, a', function() {
	var that = this;
	var htmlurl = that.getAttribute('data-url');
	var htmlid = that.getAttribute('data-id');
	var htmlval = that.getAttribute('data-val');
	var htmltype = that.getAttribute('data-view') ? that.getAttribute('data-view') : 'old';

	var styles = {
		popGesture: 'hide'
	};
	if (!htmlurl || !htmlid) {
		return;
	}

//	_UserData = JSON.parse(localStorage.getItem('$users') || '{}');
//	if ((htmlid == 'cart.html' || htmlid == 'user.html') && !isobj(_UserData)) {
//		toLoginView();
//		return;
//	}

	if (htmlid == 'index.html') {
		mui.openWindow({
			id: htmlid,
			url: htmlurl,
			show: {
				aniShow: true
			},
			waiting: {
				autoShow: true,
				title: '努力中...'
			}
		});
		return;
	}

	var para = {
		para: htmlval
	};
	if (isobj(htmlval)) {
		para = htmlval;
	}

	if (htmlid != 'user.html') {
		mui.openWindow({
			id: htmlid,
			url: htmlurl,
			createNew: true,
			show: {
				aniShow: true
			},
			styles: styles,
			waiting: {
				autoShow: true,
				title: '努力中...'
			},
			extras: para
		});
		return;
	}
	mui.openWindow({
		id: htmlid,
		url: htmlurl,
		show: {
			aniShow: true
		},
		waiting: {
			autoShow: true,
			title: '努力中...'
		}
	});



});

/*打分*/


        var $self = $(this),
            timer = null;
        $('.stars-box').children().each(function(index) {
            $(this).on("click", function() {
                $('.stars-box').children().removeClass("active");
                for (var i = 0; i < parseInt(index + 1); i++) {
                    $('.stars-box').children().eq(i).addClass("active");
					$('#star_num').val(i+1)
                }
				
            });
        });




	 
